/**
 * 数据流模型
 * @param app
 * @returns {*}
 */
module.exports = app => {
  const mongoose = app.mongoose
  const { Schema } = mongoose
  const { ObjectId } = Schema

  const modelSchema = new Schema({
    name: { type: String, required: true, remark: '数据流名称' },
    action: { type: ObjectId, ref: 'action', remark: '关联动作' },
    struct: {
      type: Object,
      required: true,
      like: {
        name: {
          type: 'string',
          remark: '数据项说明',
          min: 10,
          max: 100,
          rules: [
            { required: true, message: '依赖数据流名称' },
            { min: 10, max: 100, message: '数值异常' },
          ],
        },
      },
      remark: '数据结构模式',
    },
    server: { type: ObjectId, ref: 'server', remark: '上报服务器' },
    remark: { type: String, remark: '备注信息' },
    user: { type: ObjectId, ref: 'user', required: true, remark: '所属用户' },
  }, {
    collections: 'stream',
    timestamps: true,
  })

  modelSchema.index({ name: 1 })

  return mongoose.model('stream', modelSchema)
}